<script setup lang="tsx">
import { ContentWrap } from '@/components/ContentWrap'
import { useI18n } from '@/hooks/web/useI18n'
import { Table } from '@/components/Table'
import { ref, unref } from 'vue'
import { useTable } from '@/hooks/web/useTable'
import { useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { Search } from '@/components/Search'
import { PointHook } from '../FacOutComponents/LedgerHooks'
import LedgerApi from '@/api/equip/ledger'
import { Dialog } from '@/components/Dialog'
import EditVue from '../EquipLedger/components/Edit.vue'
import { PipePointInfoType } from '@/api/gis/type'
const { t } = useI18n()
const { crudSchemas } = PointHook()
const ActionCrudSchemas = {
  field: 'action',
  label: t('tableDemo.action'),
  width: 200,
  fixed: 'right',
  search: {
    hidden: true
  },
  form: {
    hidden: true
  },
  detail: {
    hidden: true
  },
  table: {
    slots: {
      default: (data: any) => {
        return (
          <>
            <span class="TableBtn" onClick={() => action(data.row)}>
              {t('详情')}
            </span>
            <span class="TableBtn" style="color:#f56c6c" onClick={() => delData(data.row)}>
              {t('删除')}
            </span>
          </>
        )
      }
    }
  }
}
crudSchemas.push(ActionCrudSchemas)
const ids = ref<string[]>([])

//表格内容接口
const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async () => {
    const { pageSize, currentPage } = tableState
    const i = await LedgerApi.GetLedgerRedList({
      pageIndex: unref(currentPage),
      pageSize: unref(pageSize),
      ...unref(searchParams)
    })
    return {
      list: i.rows,
      total: i.total
    }
  },
  fetchDelApi: async () => {
    const res = await LedgerApi.DeletLedger(unref(ids)[0])
    return !!res
  }
})
const { total, loading, dataList, pageSize, currentPage } = tableState
const { getList, getElTableExpose, delList } = tableMethods
const { allSchemas } = useCrudSchemas(crudSchemas)

const currentRow = ref<PipePointInfoType>()

const delLoading = ref(false)
//表格删除操作
const delData = async (row: PipePointInfoType | null) => {
  const elTableExpose = await getElTableExpose()
  ids.value = row ? [row.uuid] : elTableExpose?.getSelectionRows().map((v: any) => v.id) || []
  delLoading.value = true
  await delList(unref(ids).length).finally(() => {
    delLoading.value = false
  })
}
const dialogVisible = ref(false)
const searchParams = ref({})
const setSearchParams = (params: any) => {
  currentPage.value = 1
  searchParams.value = params
  getList()
}

//表格中操作
const action = (row: PipePointInfoType) => {
  dialogVisible.value = true //打开弹窗
  currentRow.value = row
}
</script>
<template>
  <div class="flex w-100% h-100%">
    <ContentWrap class="flex-[3]" title="红线内设施台账">
      <div class="flex">
        <Search
          :schema="allSchemas.searchSchema"
          @reset="setSearchParams"
          @search="setSearchParams"
        />
      </div>
      <Table
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :columns="allSchemas.tableColumns"
        :data="dataList"
        row-key="uuid"
        :loading="loading"
        @register="tableRegister"
        :pagination="{
          total
        }"
      />
    </ContentWrap>
    <Dialog v-model="dialogVisible" title="详情" style="width: 1200px" maxHeight="500px">
      <EditVue ref="EditRef" :current-row="currentRow" :FormDisabled="true" />
    </Dialog>
  </div>
</template>
<style scope>
.el-tree-node:focus > .el-tree-node__content {
  color: #409eff;
}
.el-card__body {
  padding: 10px;
  /* height: 90%; */
}
.CloseBtn {
  font-weight: 600;
  margin: 0 3px;
  color: darkgrey;
}

.el-collapse-item {
  padding: 15px 0;
}
.el-collapse-item__content {
  padding-bottom: 0;
}
.el-collapse-item__header {
  width: 62px;
  height: 24px;
  border-radius: 2px;
  color: white;
  background: #409eff;
  justify-content: center;
  font-size: 12px;
}
.el-collapse-item__arrow {
  margin: 0 2px;
}

.TableBtn {
  margin: 0 5px;
  cursor: pointer;
  color: #409eff;
  font-weight: 600;
}
</style>
@/views/GIS/OneMap/components/PointHooks @/api/gis/type
